package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import view.Fenetre;


public class ActionVue implements ActionListener{
	
	
	private Fenetre view;
	ArrayList<ArrayList<String>> valeurFile;
	private int finalSet = 0;
	private boolean valbutton = false;
	private static int r = 0;
	private ArrayList<String> tabValue = new ArrayList<String>();
	
	public ActionVue(Fenetre view, ArrayList<ArrayList<String>> valeurFile){
		this.view = view;
		this.valeurFile = valeurFile;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (view.getjComboSet().getSelectedItem().toString().equalsIgnoreCase("set1"))
		{
			view.setNumSet(0);
			view.getjTextFieldCallPutFlag().setText(valeurFile.get(view.getNumSet()).get(0));
			view.getjTextFieldS().setText(valeurFile.get(view.getNumSet()).get(1));
			view.getjTextFieldX().setText(valeurFile.get(view.getNumSet()).get(2));
			view.getjTextFieldT().setText(valeurFile.get(view.getNumSet()).get(4));
			view.getjTextFieldr().setText(valeurFile.get(view.getNumSet()).get(3));
			view.getjTextFieldb().setText(valeurFile.get(view.getNumSet()).get(5));
			view.getjTextFieldv().setText(valeurFile.get(view.getNumSet()).get(6));
			view.getjTextFieldnSteps().setText(valeurFile.get(view.getNumSet()).get(7));
			view.getjTextFieldnSimulations().setText(valeurFile.get(view.getNumSet()).get(8));
		}
		
		if (view.getjComboSet().getSelectedItem().toString().equalsIgnoreCase("set2"))
		{
			view.setNumSet(1);
			view.getjTextFieldCallPutFlag().setText(valeurFile.get(view.getNumSet()).get(0));
			view.getjTextFieldS().setText(valeurFile.get(view.getNumSet()).get(1));
			view.getjTextFieldX().setText(valeurFile.get(view.getNumSet()).get(2));
			view.getjTextFieldT().setText(valeurFile.get(view.getNumSet()).get(4));
			view.getjTextFieldr().setText(valeurFile.get(view.getNumSet()).get(3));
			view.getjTextFieldb().setText(valeurFile.get(view.getNumSet()).get(5));
			view.getjTextFieldv().setText(valeurFile.get(view.getNumSet()).get(6));
			view.getjTextFieldnSteps().setText(valeurFile.get(view.getNumSet()).get(7));
			view.getjTextFieldnSimulations().setText(valeurFile.get(view.getNumSet()).get(8));
		}
		
		if (view.getjComboSet().getSelectedItem().toString().equalsIgnoreCase("set3"))
		{
			view.setNumSet(2);
			view.getjTextFieldCallPutFlag().setText(valeurFile.get(view.getNumSet()).get(0));
			view.getjTextFieldS().setText(valeurFile.get(view.getNumSet()).get(1));
			view.getjTextFieldX().setText(valeurFile.get(view.getNumSet()).get(2));
			view.getjTextFieldT().setText(valeurFile.get(view.getNumSet()).get(4));
			view.getjTextFieldr().setText(valeurFile.get(view.getNumSet()).get(3));
			view.getjTextFieldb().setText(valeurFile.get(view.getNumSet()).get(5));
			view.getjTextFieldv().setText(valeurFile.get(view.getNumSet()).get(6));
			view.getjTextFieldnSteps().setText(valeurFile.get(view.getNumSet()).get(7));
			view.getjTextFieldnSimulations().setText(valeurFile.get(view.getNumSet()).get(8));
		}
		
		if (view.getjComboSet().getSelectedItem().toString().equalsIgnoreCase("set4"))
		{
			view.setNumSet(3);
			view.getjTextFieldCallPutFlag().setText(valeurFile.get(view.getNumSet()).get(0));
			view.getjTextFieldS().setText(valeurFile.get(view.getNumSet()).get(1));
			view.getjTextFieldX().setText(valeurFile.get(view.getNumSet()).get(2));
			view.getjTextFieldT().setText(valeurFile.get(view.getNumSet()).get(4));
			view.getjTextFieldr().setText(valeurFile.get(view.getNumSet()).get(3));
			view.getjTextFieldb().setText(valeurFile.get(view.getNumSet()).get(5));
			view.getjTextFieldv().setText(valeurFile.get(view.getNumSet()).get(6));
			view.getjTextFieldnSteps().setText(valeurFile.get(view.getNumSet()).get(7));
			view.getjTextFieldnSimulations().setText(valeurFile.get(view.getNumSet()).get(8));
		}
		
		if (view.getjComboSet().getSelectedItem().toString().equalsIgnoreCase("set5"))
		{
			view.setNumSet(4);
			view.getjTextFieldCallPutFlag().setText(valeurFile.get(view.getNumSet()).get(0));
			view.getjTextFieldS().setText(valeurFile.get(view.getNumSet()).get(1));
			view.getjTextFieldX().setText(valeurFile.get(view.getNumSet()).get(2));
			view.getjTextFieldT().setText(valeurFile.get(view.getNumSet()).get(4));
			view.getjTextFieldr().setText(valeurFile.get(view.getNumSet()).get(3));
			view.getjTextFieldb().setText(valeurFile.get(view.getNumSet()).get(5));
			view.getjTextFieldv().setText(valeurFile.get(view.getNumSet()).get(6));
			view.getjTextFieldnSteps().setText(valeurFile.get(view.getNumSet()).get(7));
			view.getjTextFieldnSimulations().setText(valeurFile.get(view.getNumSet()).get(8));
			
		}
		
		if (e.getSource() == view.getjButtonValidate())
		{
			/*ArrayList<String> vv = new ArrayList<String>();
			vv.add(view.getjTextFieldCallPutFlag().getText());
			vv.add(view.getjTextFieldS().getText());
			System.out.println(view.getjTextFieldX().getText());
			vv.add(view.getjTextFieldX().getText());
			vv.add(view.getjTextFieldr().getText());
			vv.add(view.getjTextFieldT().getText());
			vv.add(view.getjTextFieldb().getText());
			vv.add(view.getjTextFieldv().getText());
			vv.add(view.getjTextFieldnSteps().getText());
			vv.add(view.getjTextFieldnSimulations().getText());*/
			
			long begin = System.currentTimeMillis();
			
			if (view.getjRadioMCMulti().isSelected())
			{
				valbutton = true;
				System.out.println(valeurFile.get(view.getNumSet()));
				
				MainMCMulti mcmulti = new MainMCMulti(valeurFile.get(view.getNumSet()));
				try {
					double dd = mcmulti.MCMulti();
					System.out.println("VAL multi thread : "+ dd);
					view.setValeurFinal(view.getValeurFinal(), Double.toString(dd));
				} catch (InterruptedException | ExecutionException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
			else if (view.getjRadioMCSingle().isSelected())
			{
				valbutton = true;
				System.out.println(valeurFile.get(view.getNumSet()));
				
				MainMCSingle mcsingle = new MainMCSingle(valeurFile.get(view.getNumSet()));

				double dd1 = mcsingle.MCSingle();
				System.out.println("VAL single thread : "+ dd1);
				view.setValeurFinal(view.getValeurFinal(), Double.toString(dd1));
			}
			else
			{
				javax.swing.JOptionPane.showMessageDialog(null, "VEUILLEZ CHOISIR LE MULTI OU SINGLE THREAD MONTE CARLO");
			}
			
			long end = System.currentTimeMillis();
			float time = ((float)(end-begin))/1000f;
			
			System.out.println("TEMPS EXECUTION : "+ time);
			tabValue.add(Double.toString((double)time));
			view.setValeurSeconde(view.getValeurSeconde(), tabValue.get(r));
			r++;
			System.out.println("Temps : "+tabValue);
		}
	}

	/**
	 * @return the finalSet
	 */
	public int getFinalSet() {
		return finalSet;
	}

	/**
	 * @param finalSet the finalSet to set
	 */
	public void setFinalSet(int finalSet) {
		this.finalSet = finalSet;
	}

	/**
	 * @return the valbutton
	 */
	public boolean isValbutton() {
		return valbutton;
	}

	/**
	 * @param valbutton the valbutton to set
	 */
	public void setValbutton(boolean valbutton) {
		this.valbutton = valbutton;
	}

	/**
	 * @return the tabValue
	 */
	public ArrayList<String> getTabValue() {
		return tabValue;
	}

	/**
	 * @param tabValue the tabValue to set
	 */
	public void setTabValue(ArrayList<String> tabValue) {
		this.tabValue = tabValue;
	}
	
	
	
}
